<?php

namespace App\Console\Commands\Tools\Camera;

use App\Kuafu\Shop\Enum\CodeEnum;
use App\Kuafu\Shop\Jobs\SyncCameraJob;
use App\Kuafu\Shop\Model\CameraModel;
use App\Kuafu\Shop\Model\Shop;
use App\Kuafu\Shop\Model\ShopMonitorDataHourModel;
use App\Kuafu\Shop\Model\ShopMonitorDataModel;
use App\Kuafu\Shop\Service\CameraLibrary\CameraFactory;
use App\Kuafu\Shop\Service\CameraService;
use App\Kuafu\Shop\Service\DeviceCameraService;
use App\Kuafu\Shop\Service\ShopService;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use OSS\Core\OssException;

class FetchCameraScreenshot extends Command
{
    protected $signature   = 'tools:fetch-camera-screenshot';
    protected $description = '摄像头截图';

    public function handle()
    {
        $this->exportAllData();
    }


    /**
     * @throws OssException
     */
    private function exportAllData()
    {
//        $streamUrl = 'https://qcloud-bj01-025-srs01.ulucu.com/live/Ub35C55V3229000197WH-1-1000.m3u8?req_id=af152130e9a0e667c7b23a4f6396536a';
//        CameraService::screenShotFromStream($streamUrl, 1, 2);

//        die;
        $shopList = Shop::query()
                        ->where('status', 0)
                        ->where('shopCode', '<', 500)
                        ->get();

        $allList = [];
        foreach ($shopList as $shopInfo) {
            $list = CameraService::getCameraList($shopInfo->id);

            if (count($list) > 0) {
                // 批量获取摄像头状态
                $first                 = $list[0];
                $camera                = new CameraFactory($first->id);
                $cameraStatusListCache = Cache::remember('getDeviceStatusBatch1:' . $shopInfo->id, 7200,
                    function () use ($camera, $list) {
                        return $camera->getIns()->getDeviceStatusBatch($list->toArray());
                    });
//                $cameraStatusListCache = $camera->getIns()->getDeviceStatusBatch($list->toArray());
                $cameraStatusList = array_column($cameraStatusListCache, null, 'id');

                foreach ($list as &$item) {
                    $item['status'] = $cameraStatusList[$item['id']]['status'] ?? 0;

                    $allList[] = [
                        'shopId'     => $shopInfo->id,
                        'deviceId'   => $item->id,
                        'shopCode'   => $shopInfo->shopCode,
                        'shopName'   => $shopInfo->name,
                        'cameraName' => $item->name,
                        'status'     => $item['status'] == 0 ? '离线' : '在线',
                        'checkTime'  => date('Y-m-d H:i:s')
                    ];

                }
            }

            $this->info($shopInfo->id . ':' . $shopInfo->name . '摄像头状态获取完成');
        }

        // 循环获取视频流
        foreach ($allList as $listOne) {
            if ($listOne['status'] == '离线') {
                continue;
            }

            // 获取流
//            $deviceCameraService = new DeviceCameraService();
//            $fluency             = $deviceCameraService->getCameraIdById($listOne['deviceId'], 'hls', 2);

            $camera     = new CameraFactory($listOne['deviceId']);
            $scheme     = 'hls';
            $clarity    = CodeEnum::CLARITY_TYPE_HIGH_DEFINITION;
            $streamData = $camera->getIns()->getLiveStreaming($scheme, $clarity);

            $type = 1; // 实施
            $camera->getIns()->heartBeat($type, $scheme);

            $streamUrl = $streamData['url'];
            // 心跳处理
            dump($streamUrl);
            CameraService::screenShotFromStream($streamUrl, $listOne['shopId'], $listOne['deviceId']);
        }

        dd($allList);

    }


}
